package com.antt.cn.test2.uis.activities;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.antt.cn.test2.R;
import com.antt.cn.test2.uis.base.BaseActivity;
import com.antt.cn.test2.utils.AntLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Ant on 2017/5/15  16:27
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class Test07 extends BaseActivity {


    @BindView(R.id.ac_test07_viewpager)
    ViewPager pager;

    private myPager adapter;

    @Override
    public void initView() {
        ButterKnife.bind(this);
        adapter = new myPager(this);
        pager.setAdapter(adapter);
    }

    @Override
    public void initData() {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add("text  aksfdkasfdjasklfjasklfjaskdfadsjflasjfklas==" + i);
        }

        adapter.setData(list);
    }

    @Override
    public void initListener() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.ac_test07_layout;
    }


    class myPager extends PagerAdapter {
        private Context context;

        private List<String> list;
        private Map<Integer, View> maps;


        public myPager(Context context) {
            this.context = context;
            maps = new HashMap<>();
        }

        public void setData(List<String> data) {
            this.list = data;
            maps.clear();
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView text = new TextView(context);
            text.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
            ));
            String value = list.get(position);
            AntLog.i("data==" + value + ",   positon==" + position);
            text.setText(value);
            maps.put(position, text);
            container.addView(text);
            return text;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(maps.get(position));
        }
    }
}
